package com.example.cscot;

import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

//
// 不同activity/view上动态添加/删除UI组件的功能
//
public class LewayWindow {
    //
    private WindowManager mManager;
    //
    public int mScreenWidth;
    public int mScreenHeight;
    public int mLineHeight;
    public float mTextSize;
    public int mDefaultBlue;
    //
    public LewayWindow() {
        mManager = (WindowManager) WeiLe.mainActivity.getSystemService(Context.WINDOW_SERVICE);
        //
        Display display = mManager.getDefaultDisplay();
        mScreenWidth = display.getWidth();
        mScreenHeight = display.getHeight();
        TextView tvTmp = new TextView(WeiLe.mainActivity);
        mLineHeight = tvTmp.getLineHeight();
        mTextSize = tvTmp.getTextSize();
        tvTmp = null;
        Button btnTmp = new Button(WeiLe.mainActivity);
        Drawable background = btnTmp.getBackground();
        mDefaultBlue = 0xFF0000FF;
        if (background instanceof ColorDrawable) {
            mDefaultBlue = ((ColorDrawable) background).getColor();
        }
        btnTmp = null;
        WeiLe.gLogText += LewayHelper.TS() + mScreenWidth + "=mScreenWidth\n";
        WeiLe.gLogText += LewayHelper.TS() + mScreenHeight + "=mScreenHeight\n";
        WeiLe.gLogText += LewayHelper.TS() + mLineHeight + "=mLineHeight\n";
        WeiLe.gLogText += LewayHelper.TS() + mTextSize + "=mTextSize\n";
        WeiLe.gLogText += LewayHelper.TS() + Integer.toHexString(mDefaultBlue) + "=mDefaultBlue\n";
        //
        return;
    }

    public void removeWidget(View floatingView) {
        if (floatingView != null) {
            mManager.removeView(floatingView);
            floatingView = null;
        }
    }

    public Button addButton(int x, int y, int width, int height,
                            int bgColor, int textColor, String text) {
        // hhttps://www.renrendoc.com/paper/161716599.html 颜色代码大全，记得再加0xFF(0000FF)=blue
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
        params.format = PixelFormat.RGBA_8888;
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        params.gravity = Gravity.TOP | Gravity.START;
        //
        params.x = x;
        params.y = y;
        if (width > 0) {
            params.width = width;
        }
        if (height > 0) {
            params.height = height;
        }
        //
        Button btn = new Button(WeiLe.mainActivity);
        View floatingView = (View) btn;
        floatingView.setLayoutParams(params);   // very important
        floatingView.setBackgroundColor(0xFF000000 | bgColor);
        floatingView.setOnClickListener(v -> removeWidget(floatingView));
        floatingView.setOnLongClickListener(v -> true);
        //
        btn.setTextColor(0xFF000000 | textColor);
        btn.setText(text);
        mManager.addView(floatingView, params); // to show it
        //
        return btn;
    }
    //
    public TextView addTextView(int x, int y, int width, int height,
                                int bgColor, int textColor, String text) {
        // hhttps://www.renrendoc.com/paper/161716599.html 颜色代码大全，记得再加0xFF(0000FF)=blue
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
        params.format = PixelFormat.RGBA_8888;
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        params.gravity = Gravity.TOP | Gravity.START;
        //
        params.x = x;
        params.y = y;
        if (width > 0) {
            params.width = width;
        }
        if (height > 0) {
            params.height = height;
        }
        //
        TextView tv = new TextView(WeiLe.mainActivity);
        View floatingView = (View) tv;
        floatingView.setLayoutParams(params);   // very important
        floatingView.setBackgroundColor(0xFF000000 | bgColor);
        // floatingView.setOnClickListener(v -> removeLewayWindow(floatingView));
        floatingView.setOnLongClickListener(v -> true);
        //
        tv.setTextColor(0xFF000000 | textColor);
        tv.setText(text);
        mManager.addView(floatingView, params); // to show it
        //
        return tv;
    }
    //
    public EditText addEditText(int x, int y, int width, int height,
                                int bgColor, int textColor, String text) {
        // hhttps://www.renrendoc.com/paper/161716599.html 颜色代码大全，记得再加0xFF(0000FF)=blue
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
        params.format = PixelFormat.RGBA_8888;
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        params.gravity = Gravity.TOP | Gravity.START;
        //
        params.x = x;
        params.y = y;
        if (width > 0) {
            params.width = width;
        }
        if (height > 0) {
            params.height = height;
        }
        //
        EditText et = new EditText(WeiLe.mainActivity);
        View floatingView = (View) et;
        floatingView.setLayoutParams(params);   // very important
        floatingView.setBackgroundColor(0xFF000000 | bgColor);
        // floatingView.setOnClickListener(v -> removeLewayWindow(floatingView));
        floatingView.setOnLongClickListener(v -> true);
        //
        et.setTextColor(0xFF000000 | textColor);
        et.setText(text);
        mManager.addView(floatingView, params); // to show it
        //
        return et;
    }
    //
    public ImageView addImageView(int x, int y, int width, int height,
                                int bgColor, int idImage) {
        // hhttps://www.renrendoc.com/paper/161716599.html 颜色代码大全，记得再加0xFF(0000FF)=blue
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
        params.format = PixelFormat.RGBA_8888;
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        params.gravity = Gravity.TOP | Gravity.START;
        //
        params.x = x;
        params.y = y;
        if (width > 0) {
            params.width = width;
        }
        if (height > 0) {
            params.height = height;
        }
        //
        ImageView iv = new ImageView(WeiLe.mainActivity);
        View floatingView = (View) iv;
        floatingView.setLayoutParams(params);   // very important
        floatingView.setBackgroundColor(0xFF000000 | bgColor);
        floatingView.setOnClickListener(v -> removeWidget(floatingView));
        floatingView.setOnLongClickListener(v -> true);
        //
        // ....
        iv.setImageResource(idImage);
        mManager.addView(floatingView, params); // to show it
        //
        return iv;
    }
    //
} // LewayWindow